package dl.jf.web.action.auth;

import java.util.List;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import dl.jf.common.constants.Constants;
import dl.jf.common.model.LabelValue;
import dl.jf.common.model.MRecord;
import dl.jf.service.iservice.IRecordService;
import dl.jf.web.action.BaseAction;
import dl.jf.web.form.RecordCreateForm;
import dl.jf.web.form.RecordForm;
import dl.jf.web.util.Toolkit;

public class RecordAction extends BaseAction {

	public RecordAction() {
		super("RecordAction");
	}
	
	// form
	private RecordForm recordForm;

	public RecordForm getRecordForm() {
		return recordForm;
	}

	public void setRecordForm(RecordForm recordForm) {
		this.recordForm = recordForm;
	}

	// service
	private IRecordService recordService;

	public void setRecordService(IRecordService recordService) {
		this.recordService = recordService;
	}

	@Override
	public String execute() throws Exception {
		return initial();
	}

	public String initial() throws Exception {
		if(recordForm == null) {
			setRecordForm(new RecordForm());
		}
		if("1".equals(recordForm.getSearchFlgHid())){
			recordForm.setInputRecordId(recordForm.getInputRecordIdHid());
			recordForm.setInputSubject(recordForm.getInputSubjectHid());
			recordForm.setRecordDateFrom(recordForm.getRecordDateFromHid());
			recordForm.setRecordDateTo(recordForm.getRecordDateToHid());
			recordForm.setSelectedPrivacy(recordForm.getSelectedPrivacyHid());
			recordForm.setSelectedType1(recordForm.getSelectedType1Hid());
			recordForm.setSelectedType2(recordForm.getSelectedType2Hid());
			return search();
		}
		pushList();
		return SUCCESS;
	}

	public String search() throws Exception {
		pushList();
		MRecord recordParam = new MRecord();
		if (!Toolkit.isEmpty(recordForm.getInputRecordId())) {
			recordParam.setRecordId(recordForm
					.getInputRecordId());
		}
		if (!Toolkit.isEmpty(recordForm.getSelectedType1())) {
			recordParam.setType1Code(Integer.parseInt(recordForm
					.getSelectedType1()));
		}
		if (!Toolkit.isEmpty(recordForm.getSelectedType2())) {
			recordParam.setType2Code(Integer.parseInt(recordForm
					.getSelectedType2()));
		}
		if (!Toolkit.isEmpty(recordForm.getSelectedPrivacy())) {
			recordParam.setPrivacyCode(Integer.parseInt(recordForm
					.getSelectedPrivacy()));
		}

		if (!Toolkit.isEmpty(recordForm.getRecordDateFrom())) {
			recordParam.setRecordDateFrom(recordForm
					.getRecordDateFrom());
		}

		if (!Toolkit.isEmpty(recordForm.getRecordDateTo())) {
			recordParam.setRecordDateTo(recordForm
					.getRecordDateTo());
		}
		recordParam.setSubject(recordForm.getInputSubject());
		List<MRecord> recordList = recordService.getRecordList(recordParam);
		for(MRecord item : recordList) {
			item.setType1Value(getCodeContent(Constants.RECORD_TYPE1_CODE_ID, item.getType1Code()));
			item.setType2Value(getCodeContent(Constants.RECORD_TYPE2_CODE_ID, item.getType2Code()));
			item.setPrivacyValue(getCodeContent(Constants.RECORD_PRIVACY_CODE_ID, item.getPrivacyCode()));
		}
		recordForm.setRecordList(recordList);
		recordForm.setInputRecordIdHid(recordForm.getInputRecordId());
		recordForm.setInputSubjectHid(recordForm.getInputSubject());
		recordForm.setRecordDateFromHid(recordForm.getRecordDateFrom());
		recordForm.setRecordDateToHid(recordForm.getRecordDateTo());
		recordForm.setSelectedPrivacyHid(recordForm.getSelectedPrivacy());
		recordForm.setSelectedType1Hid(recordForm.getSelectedType1());
		recordForm.setSelectedType2Hid(recordForm.getSelectedType2());
		recordForm.setSearchFlgHid("1");
		return SUCCESS;
	}

	public String newRecord() throws Exception {
		return Constants.ACTION_RESULT_CREATE;
	}

	private void pushList() {
		recordForm
				.setType1List(getCodeLabelListById(Constants.RECORD_TYPE1_CODE_ID));
		recordForm
				.setType2List(getCodeLabelListById(Constants.RECORD_TYPE2_CODE_ID));
		recordForm
				.setPrivacyList(getCodeLabelListById(Constants.RECORD_PRIVACY_CODE_ID));
		recordForm
				.setPrivacyList(getCodeLabelListById(Constants.RECORD_PRIVACY_CODE_ID));
	}

	private String getCodeContent(Integer codeId, Integer code) {
		List<LabelValue> list = getCodeLabelListById(codeId);
		String result = "";
		for(LabelValue lv : list) {
			if(code == Integer.parseInt(lv.getValue())) {
				result = lv.getLabel();
				break;
			}
		}
		return result;
	}

//	private void getSession() {
//		session = ActionContext.getContext().getSession();
//		request = ServletActionContext.getRequest();
//	}
}
